package com.sys.web.head.controller;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sys.common.controller.BaseController;
import com.sys.common.pojo.ApiResponse;
import com.sys.common.pojo.ApiResponseEnum;
import com.sys.common.util.ConstatFinalUtil;
import com.sys.users.pojo.Customer;
import com.sys.users.service.IUsersDbService;

/**
 * 	用户首页操作
 * 	@author 梦缠绵
 */
@Controller
@RequestMapping("/head/users/")
public class UsersHeadController extends BaseController
{
	@Resource
	private IUsersDbService usersDbService;
	/**
	 * 首页
	 * @return
	 */
	@RequestMapping("/main")
	public String main()
	{
		ConstatFinalUtil.SYS_LOGGER.info("--main--");
		return "redirect:/main.htm";
	}
	
	/**
	 * 打开用户更新
	 * @return
	 */
	@RequestMapping("/usersUpdate")
	public String usersUpdate(HttpServletRequest request,String updateFlag ,Model model)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--usersUpdate--");
		String operType = request.getParameter("operType");
		/* 从session中获取信息 */
		Customer customer = (Customer) this.findObjfromSession(request, "users");
		/* 准备请求的信息 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("id", customer.getId() + "");
		ApiResponse<Customer> apiResponse = this.usersDbService.findOneCustomerService(condMap);
		
		/* 清除信息 */
		if(updateFlag == null || "".equalsIgnoreCase(updateFlag))
		{
			/* 清除信息 */
			apiResponse.clearInfo();
		}
		model.addAttribute("response", apiResponse.toJSON());
		if("updateInfo".equalsIgnoreCase(operType))
		{
			/* 根据不同的请求跳转不同的页面 */
			return "/head/users/usersUpdateInfo";
		}else if("updatePass".equalsIgnoreCase(operType))
		{
			/* 根据不同的请求跳转不同的页面 */
			return "/head/users/usersUpdatePass";
		}
		return "/head/users/usersInfo";
	}
	
	/**
	 * 打开用户提交
	 * @return
	 */
	@RequestMapping("/usersUpdateSubmit")
	public String usersUpdateSubmit(HttpServletRequest request,Model model)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--usersUpdateSubmit--");
		ApiResponse<Object> apiResponseRes = new ApiResponse<Object>();
		
		String operType = request.getParameter("operType");
		/* 从session中获取信息 */
		Customer customer = (Customer) this.findObjfromSession(request, "users");
		
		/* 准备请求的信息 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("id", customer.getId() + "");
		ApiResponse<Customer> usersApiResponse = this.usersDbService.findOneCustomerService(condMap);
		customer = usersApiResponse.getDataOneJava();
		
		if("updateInfo".equalsIgnoreCase(operType))
		{
			/* 用户填写的信息 */
			String account = request.getParameter("account");
			String password = request.getParameter("password");
			String phone = request.getParameter("phone");
			String name = request.getParameter("name");
			
			/* 赋值 */
			customer.setAccount(account);
			customer.setPassword(password);
			customer.setPhone(phone);
			customer.setName(name);
			
			ApiResponse<Object> apiResponse = this.usersDbService.updateOneCustomerService(customer);
			model.addAttribute("response", apiResponse.toJSON());
			/* 根据不同的请求跳转不同的页面 */
			request.getSession().setAttribute("users", customer);
			return "/head/users/usersUpdateInfo";
		}else if("updatePass".equalsIgnoreCase(operType))
		{
			String souPass = request.getParameter("souPass");
			String password = request.getParameter("password");
			
			if(this.encryptUtil.checkStr(souPass, customer.getPassword()))
			{
				customer.setPassword(this.encryptUtil.encodeStr(password));
				ApiResponse<Object> apiResponse = this.usersDbService.updateOneCustomerService(customer);
				model.addAttribute("response", apiResponse.toJSON());
				request.getSession().setAttribute("users", customer);
			}else
			{
				apiResponseRes.setCode(ApiResponseEnum.INFO_SOUPASS_INCORRECT.getStatus());
				apiResponseRes.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponseRes.getCode() + ""),Collections.emptyMap());
				model.addAttribute("response", apiResponseRes.toJSON());
			}
			/* 根据不同的请求跳转不同的页面 */
			return "/head/users/usersUpdatePass";
		}
		return "/head/users/usersInfo";
	}
	
	/**
	 * 退出页面
	 * @return
	 */
	@RequestMapping("/logout")
	public String logout(HttpServletRequest request, HttpSession session)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--logout--");
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		/* 获取用户 */
		Customer users = (Customer) this.findObjfromSession(request, "users");
		try
		{
			String userType = session.getAttribute("userType") + "" ; 
			if("1".equalsIgnoreCase(userType))
			{
				/* 做一些清除的操作 */
				session.removeAttribute("customer");
			}else if("2".equalsIgnoreCase(userType))
			{
				/* 做一些清除的操作 */
				session.removeAttribute("scenicSpotAdmin");
			}else if("3".equalsIgnoreCase(userType))
			{
				/* 做一些清除的操作 */
				session.removeAttribute("superSsa");
			}
			apiResponse.setCode(ApiResponseEnum.INFO_LOGOUT.getStatus()); 
		} catch (Exception e)
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_SERVER_ERROR.getStatus()); 
			ConstatFinalUtil.SYS_LOGGER.error("退出报错了;email:{}", users.getAccount(),e);
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		/* 提示信息 */
		session.setAttribute("response", apiResponse.toJSON());
		/* 客户端跳转到登陆页面 */
		return "redirect:/login.htm";
	}
}
